package com.seelyn.ddvideo.service;

import com.seelyn.ddvideo.model.VideoPrefix;
import com.seelyn.ddvideo.repository.VideoPrefixRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ifcousing-linfeng
 * @since 2017/6/15
 */
@Service
@CacheConfig(cacheNames = "video_prefix_cache")
public class VideoPrefixService {

    @Autowired
    private VideoPrefixRepository videoPrefixRepository;

    @Cacheable(key = "'findByFromSite_' + #p0")
    public VideoPrefix findByFromSite(String fromSite) {
        return videoPrefixRepository.findByFromSite(fromSite);
    }

    @Caching(
            evict = @CacheEvict(key = "'findAllVideoPrefix'"),
            put = @CachePut(key = "'findByFromSite_' + #p0.fromSite")
    )
    public VideoPrefix saveVideoPrefix(VideoPrefix prefix) {
        return videoPrefixRepository.save(prefix);
    }

    @Cacheable(key = "'findAllVideoPrefix'")
    public List<VideoPrefix> findAll() {
        return videoPrefixRepository.findAll();
    }

    @CacheEvict
    public void deleteAll() {
        videoPrefixRepository.deleteAll();
    }

}
